import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from '../views/Register.vue'
import Login from '../views/Login'
import More from "../views/main/More";
import Layout from "../views/Layout";
import NoteShelf from "../views/main/Noteshelf";
import NoteEdit from "../views/notedetail/NoteEdit";
import NoteContent from "../views/notedetail/NoteContent";

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import MavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import store from "../store";

Vue.use(VueRouter)
Vue.use(ElementUI)
Vue.use(MavonEditor)

  const routes = [
  {
    path: '/',
    name: '登录',
    component: Login
  },
  {
    path: '/register',
    name: '注册',
    component: Register
  },
  {
    path: '/note',
    component: Layout,
    redirect: '/note/noteshelf',
    children: [
      {
        path: 'noteshelf',
        name: '我的笔记',
        component: NoteShelf,
        meta: {
          isAuthc: true
        }
      },
      {
        path: 'noteDetail/:noteId',
        name: '笔记详情',
        component: NoteEdit,
        isNotActive: true,
        meta: {
          isAuthc: true
        }
      },
      {
        path: 'noteContent/:noteId',
        name: '笔记内容',
        component: NoteContent,
        isNotActive: true,
        meta: {
          isAuthc: true
        }
      },
      {
        path: 'more',
        name: '更多功能',
        component: More,
        meta: {
          isAuthc: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(((to, from, next) => {
  if (to.meta.isAuthc) {
    if (store.state.currentUser) {
      next()
    } else {
      next({
        path: '/'
      })
    }
  } else {
    next()
  }
}))

export default router
